package fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.example.admintor.humaninteraction.Data;
import com.example.admintor.humaninteraction.LoginActivity;
import com.example.admintor.humaninteraction.R;

import java.lang.reflect.Field;

/**
 * Created by Admintor on 2017/11/9.
 * 迎宾探测器
 */

public class TenFragment extends Fragment implements View.OnClickListener {
    ImageView hi;
    Button btn;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    if (Data.REN_TI_STR.equals("100")) {
                        LoginActivity.landing.setKeyByUiid(Data.MEN_LING, 1);
                        hi.setBackgroundResource(R.mipmap.p_down);
                    } else {
                        hi.setBackgroundResource(R.mipmap.p_up);
                    }
                    break;
            }
        }
    };
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.sendEmptyMessage(0x123);
            handler.postDelayed(this, 1000);
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_ten, container, false);
        hi = (ImageView) view.findViewById(R.id.hi);
        btn = (Button) view.findViewById(R.id.btnqk);
        btn.setOnClickListener(this);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        handler.postDelayed(runnable, 1000);
    }

    @Override
    public void onStop() {
        super.onStop();
        handler.removeCallbacks(runnable);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);


        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    private boolean state=true;

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnqk:
                if (state) {
                    LoginActivity.landing.setVariableByUiid(Data.KAI_GUAN,"1", 1);
                    state = false;
                } else {
                    LoginActivity.landing.setVariableByUiid(Data.KAI_GUAN,"1" ,0);
                    state = true;
                }
                break;
        }
    }
}
